#!/bin/sh
url=$1
port=$2
account=$3
password=$4
ftp_path=$5
dev_path=$6
mode=$7 #1:download,2:upload
sftp_download()
{
    expect <<- EOF
    set timeout 20
    spawn sftp -oPort=${port} ${account}@${url}
    expect {
        "*(yes/no)?" {send "yes\n"; exp_continue}
        "*password:" {send "${password}\n"}
    }
    expect "sftp>"
    send "get ${ftp_path}\n"
    expect "sftp>"
    send "bye\n"
EOF
}

sftp_upload()
{
    expect <<- EOF
    set timeout 20
    spawn sftp -oPort=${port} ${account}@${url}
    expect {
        "*(yes/no)?" {send "yes\n"; exp_continue}
        "*password:" {send "${password}\n"}
    }
    expect "sftp>"
    send "put ${dev_path} ${ftp_path}\n"
    expect "sftp>"
    send "bye\n"
EOF
}

main()
{
    if [ $mode -eq 1 ]; then #download
        mkdir -p ${dev_path}
        cd $dev_path
        sftp_download
        cd -
    else #upload
        sftp_upload
    fi
}

main